package com.veryUsedCar.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.veryUsedCar.entity.Charnnel;
import com.veryUsedCar.mapper.CharnnelMapper;
import com.veryUsedCar.service.CharnnelService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CharnnelServiceImpl implements CharnnelService {
    @Resource
    private CharnnelMapper charnnelMapper;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<Charnnel> findCharnnel() {
        //先判断redis中是否包含key
        if (redisTemplate.hasKey("charnnel")) {
            String charnnel = (String) redisTemplate.opsForValue().get("charnnel");//如果有就获得
            List<Charnnel> fcCharnnel = JSONObject.parseArray(charnnel, Charnnel.class);
            return fcCharnnel;
        } else {//没有就去数据库查
            List<Charnnel> list = charnnelMapper.selectList(null);//查询总的导航栏
            String s = JSONObject.toJSONString(list);//把集合转换为string
            redisTemplate.opsForValue().set("charnnel", s);//没有就增加到redis中
            return list;

        }


    }
}
